package com.waterworld.watch.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.AddressBookBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.PhotoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by nhuan on 2019/4/10.
 */

public class SingleListenAdapter extends RecyclerView.Adapter<SingleListenAdapter.MyHolder> {
    private Context mContext;
    private List<AddressBookBean> mList;

    public SingleListenAdapter(Context mContext, List<AddressBookBean> mData) {
        this.mContext = mContext;
        this.mList = mData;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.item_single_listen, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        if (mList.get(i).getHead() == null) {
            Glide.with(mContext).load(R.drawable.headimage_default).into(myHolder.ivAvatar);
        }else if (mList.get(i).getHead().startsWith("sys")) {
            Glide.with(mContext).load(PhotoUtils.getResource(mList.get(i).getHead().substring(0, 13))).into(myHolder.ivAvatar);
        } else {
            Glide.with(mContext).load(UrlContants.FILE_URL + "resources/watch/" + mList.get(i).getHead()).into(myHolder.ivAvatar);
        }
        if (mList.get(i).getName() != null) {
            myHolder.name.setText(mList.get(i).getName());
        }
        if (mList.get(i).getPhone() != null) {
            myHolder.phone.setText(mList.get(i).getPhone());
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_avatar)
        CircleImageView ivAvatar;

        @BindView(R.id.name)
        TextView name;

        @BindView(R.id.phone)
        TextView phone;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
